//
//  ACMsgSendView.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/4/7.
//

#import "ACMsgSendView.h"


@interface ACMsgSendView ()<UITextViewDelegate>

@property(nonatomic,strong) UITextView *msgTextView;
@property(nonatomic,strong) UIButton *msgSendBtn;
@property(nonatomic,strong) RACDisposable *keyboardChangeFrameDisposable;

@end


@implementation ACMsgSendView

-(UITextView *)msgTextView{
    if (!_msgTextView) {
        _msgTextView = [[UITextView alloc] init];
        _msgTextView.text = @"";
        _msgTextView.font = [UIFont systemFontOfSize:14.0];
        [_msgTextView wgk_addPlaceHolder:@"输入消息发送~"];
        _msgTextView.wgk_placeHolderTextView.font = [UIFont systemFontOfSize:14];
        _msgTextView.wgk_placeHolderTextView.textColor = UIColorFromRGB(0xC0C4CC);
        _msgTextView.backgroundColor = UIColorFromRGB(0xffffff);
        _msgTextView.layer.masksToBounds = YES;
        _msgTextView.layer.cornerRadius = 4;
        _msgTextView.returnKeyType = UIReturnKeySend;
        _msgTextView.delegate = self;
    }
    return _msgTextView;
}
- (void)dealloc
{
    [self.keyboardChangeFrameDisposable dispose];
}
-(instancetype)init{
    if (self = [super init]) {
        [self setUI];
        [self addObserver];
    }
    return self;
}
-(void)setUI{
    self.backgroundColor = UIColorFromRGB(0xF4F5F8);
    
    self.msgSendBtn = [[UIButton alloc] initWithFrame:CGRectZero];
    self.msgSendBtn.layer.masksToBounds = YES;
    self.msgSendBtn.layer.cornerRadius = 17;
    [self.msgSendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.msgSendBtn wgk_setBackgroundColor:UIColorFromRGB(0xFEAFA3) forState:UIControlStateDisabled];
    [self.msgSendBtn wgk_setBackgroundColor:UIColorFromRGB(0xFF6048) forState:UIControlStateNormal];
    self.msgSendBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.msgSendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [self addSubview:self.msgSendBtn];
    [self.msgSendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@33);
        make.width.mas_equalTo(@60);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(-10);
    }];
    
    [self addSubview:self.msgTextView];
    [self.msgTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.equalTo(self.msgSendBtn.mas_left).offset(-15);
        make.bottom.mas_offset(-10);
        make.top.mas_offset(10);
    }];
   
    RAC(self.msgSendBtn,enabled) = [[RACObserve(self.msgTextView, text) merge:self.msgTextView.rac_textSignal] map:^id _Nullable(NSString*  _Nullable value) {
        NSString *Str = [NSString stringWithString:[value stringByReplacingOccurrencesOfString:@" " withString:@""]];
        return @(Str.length > 0);
    }];
    @weakify(self);
    [[self.msgSendBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if (self.textHandler) {
            self.textHandler(self.msgTextView.text);
        }
        self.msgTextView.text = @"";
        [self updateTextViewHeight:self.msgTextView];
    }];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]) {
        [self sizeToFit];
        if (self.textHandler) {
            self.textHandler(textView.text);
        }
        self.msgTextView.text = @"";
        [self updateTextViewHeight:textView];
        return  NO;
    }else{
        [self updateTextViewHeight:textView];
    }
    return YES;
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    if (!self.isOutScreen) {
        [self.msgTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(-10);
        }];
        [self.msgSendBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(-10);
        }];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(53);
        }];
    }
}
-(void)textViewDidEndEditing:(UITextView *)textView{
    if (!self.isOutScreen) {
        [self.msgTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(-10 - ACSafeAreaBottomHeight);
        }];
        [self.msgSendBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(-10 - ACSafeAreaBottomHeight);
        }];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(53 + ACSafeAreaBottomHeight);
        }];
    }
}
-(void)updateTextViewHeight:(UITextView *)textView{
    CGFloat maxHeight = 100.0;
    CGSize constrainsize = CGSizeMake((ACSCREEN_WIDTH - 105), CGFLOAT_MAX);
    CGSize size = [textView sizeThatFits:constrainsize];
    if (size.height >= maxHeight) {
        textView.scrollEnabled = YES;
    }else{
        textView.scrollEnabled = NO;
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(size.height + 20 );
        }];
    }
}
-(void)sizeFit{
    CGSize constrainsize = CGSizeMake((ACSCREEN_WIDTH - 105), CGFLOAT_MAX);
    CGSize size = [self.msgTextView sizeThatFits:constrainsize];
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(size.height + 20 );
    }];
}
-(void)addObserver{
    @weakify(self);
    self.keyboardChangeFrameDisposable = [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self)
        DDLogInfo(@"键盘高度发生变化%@",x);
        NSDictionary *dicInfo = x.userInfo;
        CGRect rect = [[dicInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGFloat changeY = rect.origin.y - ACSCREEN_HEIGHT;
        double duration = [[dicInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        DDLogInfo(@"%f---------changey",changeY);
        if (changeY < 0) {
            // 键盘
            [UIView animateWithDuration:duration animations:^{
                [self mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.bottom.offset(changeY);
                }];
            }];
        }else{
            [UIView animateWithDuration:duration animations:^{
                [self mas_updateConstraints:^(MASConstraintMaker *make) {
                    if (self.isOutScreen) {
                        make.bottom.offset(changeY + ACSafeAreaBottomHeight + 53 + 10);
                    }else{
                        make.bottom.offset(changeY );
                    }
                }];
            }];
        }
    }];
    
}
-(void)becameFirstResponder{
    [self.msgTextView becomeFirstResponder];
}
-(void)cancelFirstResponder{
    [self.msgTextView endEditing:YES];
}
- (void)setIsOutScreen:(BOOL)isOutScreen{
    _isOutScreen = isOutScreen;
    if (!self.isOutScreen) {
        [self.msgTextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(-10 - ACSafeAreaBottomHeight);
        }];
        [self.msgSendBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(-10 - ACSafeAreaBottomHeight);
        }];
    }
}
@end
